How to post JSON object to a URL without cURL in PHP? [closed]

Posted by empyreanphoenix on Programmers See other posts from Programmers or by empyreanphoenix
Published on 2012-08-29T04:27:27Z Indexed on 2012/08/29 9:49 UTC
Read the original article Hit count: 169

Filed under:
|
|

I have written a send sms code which uses an available sms api. So, I need to post a string in the json(string in $json) format to a url(url specified in $url var), but I am getting the following error

String index out of range: -1 I understand that the error arises when requesting for an index that isn't there, but I dont find how that applies in this case. Please help.

Note: name1,name2 and name3 are sender name, phone number and content respectively. Thanks

 
 array (
                    'method' => 'POST',
                    'content' => $data,
                    'header' =>"Authentication:$key" . "timeout:50000"."ContentLength: " . strlen ( $data) . "\r\n" 
) 
    );
    if ($optional_headers != null) 
    {
        $params ['http'] ['header'] = $optional_headers;
    }
    $ctx = stream_context_create ( $params );
    try 
    {
        $fp = fopen ( $url, 'rb', false, $ctx );
        $response = stream_get_contents ( $fp );
        echo "response";
    } catch ( Exception $e ) {
        echo 'Exception: ' . $e->getMessage ();
        echo "caught";
    }
     echo "done";
     return $response;
}
  

© Programmers or respective owner

Related posts about php

Related posts about http